package hlhj.fhp.checkpoint.customView

import android.content.Context
import android.support.v7.widget.GridLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.PopupWindow
import com.tencent.bugly.proguard.p
import hlhj.fhp.checkpoint.R
import hlhj.fhp.checkpoint.adp.ChooseLocPopAdp
import hlhj.fhp.checkpoint.adp.ChooseZMPopAdp
import hlhj.fhp.checkpoint.javaBean.LocationBean
import kotlinx.android.synthetic.main.pop_choose_loc.view.*

/**
 * Created by Never Fear   on 2018\11\8 0008.
Never More....
成日斗前斗后
想走都难自己走
是我打勒仗自作自受

  //type 1 地方 2 字母
 */
class ChooseLocationPop(private val c:Context) :PopupWindow() {
    private var rootView: View?=null
    private var  choosLocPopAdp: ChooseLocPopAdp?=null
    private var choosAmAdp:ChooseZMPopAdp?=null
    private val datas=ArrayList<LocationBean.InfoBean>()
    private var tempStr=""
    private var firstStr=""
    private var secStr=""
    var clickListener:OnTagClickListener?=null
    init {
        this.width=LinearLayout.LayoutParams.WRAP_CONTENT
        this.height=LinearLayout.LayoutParams.WRAP_CONTENT
        this.isOutsideTouchable=true
        this.isFocusable=true
        init()
    }
    private fun init() {
        rootView=LayoutInflater.from(c).inflate(R.layout.pop_choose_loc,null)
        contentView=rootView
        this.animationStyle= R.style.rulePopStyle

        setList()

    }

    private fun setList() {
            choosLocPopAdp = ChooseLocPopAdp(datas)
            contentView.locRecy.adapter = choosLocPopAdp
            contentView.locRecy.layoutManager = GridLayoutManager(c, 5)
            choosLocPopAdp?.setOnItemClickListener { adapter, view, position ->
                tempStr=""
                firstStr=datas[position].city_prefix
                tempStr=datas[position].city_prefix

                val plate_prefix = datas[position].plate_prefix
                if (plate_prefix.size==1&&plate_prefix[0].equals("")||plate_prefix.size==0){
                    val zmDatas=ArrayList<String>()
                    doFake(zmDatas)
                    choosAmAdp = ChooseZMPopAdp(zmDatas)
                    contentView.locRecy.adapter = choosAmAdp
                    contentView.locRecy.layoutManager = GridLayoutManager(c, 5)
                    choosAmAdp?.setOnItemClickListener { adapter, view, position ->
                        tempStr=tempStr+ zmDatas[position]
                        secStr=zmDatas[position]
                        clickListener?.click(tempStr,firstStr,"")
                        dismiss()
                    }
                }else{
                    choosAmAdp = ChooseZMPopAdp(plate_prefix as ArrayList<String>)
                    contentView.locRecy.adapter = choosAmAdp
                    contentView.locRecy.layoutManager = GridLayoutManager(c, 5)
                    choosAmAdp?.setOnItemClickListener { adapter, view, position ->
                        tempStr=tempStr+ plate_prefix[position]
                        secStr=plate_prefix[position]

                        clickListener?.click(tempStr,firstStr,secStr)
                        dismiss()
                    }
                }

            }
    }

    private fun doFake(zmDatas: ArrayList<String>) {
        zmDatas.add("A");
        zmDatas.add("B");
        zmDatas.add("C");
        zmDatas.add("D");
        zmDatas.add("E");
        zmDatas.add("F");
        zmDatas.add("G");
        zmDatas.add("H");
        zmDatas.add("I");
        zmDatas.add("J");
        zmDatas.add("K");
        zmDatas.add("L");
        zmDatas.add("M");
        zmDatas.add("N");
        zmDatas.add("O");
        zmDatas.add("P");
        zmDatas.add("Q");
        zmDatas.add("R");
        zmDatas.add("S");
        zmDatas.add("T");
        zmDatas.add("U");
        zmDatas.add("V");
        zmDatas.add("W");
        zmDatas.add("X");
        zmDatas.add("Y");
        zmDatas.add("Z");
    }

    fun refresh(data:ArrayList<LocationBean.InfoBean>){
        datas.clear()
        datas.addAll(data)
        setList()
    }

    interface OnTagClickListener{
        fun click(name:String,fStr:String,sStr:String)
    }

}